package leecode;

import java.util.*;

/**
 * 第六天
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

//第二题
public class Leetcode7{
    public int[] levelOrder1(TreeNode root) {
        if(root==null){
            return new int[0];
        }
        Queue<TreeNode>queue=new LinkedList<>();
        queue.add(root);
        List <Integer>list=new ArrayList();
        int i=0;
        while (!queue.isEmpty()){
            TreeNode tmp=queue.poll();
            list.add(tmp.val);
            if(tmp.left!=null)queue.add(tmp.left);
            if(tmp.right!=null)queue.add(tmp.right);
        }
        int []arr=new int[list.size()];
        for (int j = 0; j < list.size(); j++) {
            arr[j]=list.get(j);
        }
        return arr;
    }
    public List<List<Integer>> levelOrder(TreeNode root) {
        List <List<Integer>>list=new ArrayList();
        if(root==null){
            return list;
        }
        Queue<TreeNode>queue=new LinkedList<>();
        queue.add(root);
        int i=0;
        while (!queue.isEmpty()){
            int k=queue.size();
            List <Integer>list1=new ArrayList();
            while (k-->0) {
                TreeNode tmp = queue.poll();
                list1.add(tmp.val);
                if (tmp.left != null) queue.add(tmp.left);
                if (tmp.right != null) queue.add(tmp.right);
            }
            list.add(list1);
        }

        return list;
    }
    //第三题
    public List<List<Integer>> levelOrder2(TreeNode root) {
        List<List<Integer>>list=new ArrayList<>();
        Deque<TreeNode> deque=new LinkedList();
        if(root==null){
            return list;
        }
        deque.add(root);
        int sign=0;
        while (!deque.isEmpty()){
            int count=deque.size();
            List<Integer>list1=new ArrayList<>();
            while (count-->0){
                if(sign%2==0){
                    TreeNode tmp=deque.pollFirst();
                    list1.add(tmp.val);
                    if(tmp.left!=null)  deque.addLast(tmp.left);
                    if(tmp.right!=null) deque.addLast(tmp.right);
                }else{
                    TreeNode tmp=deque.pollLast();
                    list1.add(tmp.val);
                    if(tmp.right!=null) deque.addFirst(tmp.right);
                    if(tmp.left!=null)  deque.addFirst(tmp.left);
                }
            }
            sign++;
            list.add(list1);
        }
        return list;
    }

    public static void main(String[] args) {
        TreeNode treeNode1=new TreeNode(3);
        TreeNode treeNode2=new TreeNode(9);
        TreeNode treeNode3=new TreeNode(20);
        TreeNode treeNode4=new TreeNode(15);
        TreeNode treeNode5=new TreeNode(7);
        treeNode1.left=treeNode2;
        treeNode1.right=treeNode3;
        treeNode3.left=treeNode4;
        treeNode3.right=treeNode5;
        Leetcode7 leetcode20=new Leetcode7();
        leetcode20.levelOrder2(treeNode1);
    }
}
